package com.pkk.components.util.file;

import com.qiniu.cdn.CdnManager;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import lombok.Generated;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description:
 * @author: peikunkun
 * @create: 2018-10-25 18:11
 **/
public class QiniuUploadUtils {

  @Generated
  private static final Logger log = LoggerFactory.getLogger(QiniuUploadUtils.class);
  private Auth auth;
  private UploadManager uploadManager;
  private BucketManager bucketManager;
  private CdnManager cdnManager;
  private String ACCESS_KEY = "3hB2VlhVE-3S01pbc";
  private String SECRET_KEY = "8ryrQEcawbBmxw6JJ";
  private String baseUrl;
  private String bucket;

  public QiniuUploadUtils(String baseUrl, String bucket) {
    this.baseUrl = baseUrl;
    this.bucket = bucket;
    this.auth = Auth.create(this.ACCESS_KEY, this.SECRET_KEY);
    Configuration cfg = new Configuration(Zone.zone2());
    this.uploadManager = new UploadManager(cfg);
    this.bucketManager = new BucketManager(this.auth, cfg);
    this.cdnManager = new CdnManager(this.auth);
  }

  public String fetch(String url) {
    try {
      FetchRet ret = this.bucketManager.fetch(url, this.bucket);
      return this.baseUrl + ret.key;
    } catch (QiniuException var3) {
      log.error("[QiniuUploadUtils.fetch:QiniuException]", var3);
      return "";
    }
  }

  public String uploadFile(String filePath, String targetName) {
    return this.upload(this.File2byte(filePath), targetName, this.getUpToken1(targetName)) ? this.baseUrl + targetName
        : "";
  }

  public String uploadFile(byte[] fileByte, String targetName) {
    return this.upload(fileByte, targetName, this.getUpToken1(targetName)) ? this.baseUrl + targetName : "";
  }

  public String uploadFile(InputStream inStream, String targetName) {
    return this.upload(this.input2byte(inStream), targetName, this.getUpToken1(targetName)) ? this.baseUrl + targetName
        : "";
  }

  public String downloadAndUpload(String urlString, String filename) {
    log.info("urlString：" + urlString + " - filename：" + filename);
    String returnUrl = "";

    try {
      URL url = new URL(urlString);
      URLConnection con = url.openConnection();
      con.setConnectTimeout(30000);
      InputStream is = con.getInputStream();
      if (this.upload(this.input2byte(is), filename, this.getUpToken0())) {
        returnUrl = this.baseUrl + filename;
      }

      is.close();
    } catch (Exception var7) {
      log.error("[QiniuUploadUtils.downloadAndUpload:Exception]", var7);
    }

    return returnUrl;
  }

  public String downloadAndUpload(byte[] data, String filename) {
    String returnUrl = "";

    try {
      if (this.upload(data, filename, this.getUpToken0())) {
        returnUrl = this.baseUrl + filename;
      }
    } catch (Exception var5) {
      log.error("[QiniuUploadUtils.downloadAndUpload2:Exception]", var5);
    }

    return returnUrl;
  }

  public byte[] File2byte(String filePath) {
    byte[] buffer = null;

    try {
      File file = new File(filePath);
      FileInputStream fis = new FileInputStream(file);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] b = new byte[1024];

      int n;
      while ((n = fis.read(b)) != -1) {
        bos.write(b, 0, n);
      }

      fis.close();
      bos.close();
      buffer = bos.toByteArray();
    } catch (FileNotFoundException var8) {
      var8.printStackTrace();
      log.error("[QiniuUploadUtils.File2byte:FileNotFoundException]", var8);
    } catch (IOException var9) {
      log.error("[QiniuUploadUtils.File2byte:IOException]", var9);
    }

    return buffer;
  }

  private String getUpToken0() {
    return this.auth.uploadToken(this.bucket);
  }

  public void deleteFile(String key) {
    try {
      this.bucketManager.delete(this.bucket, key);
    } catch (QiniuException var3) {
      log.error("[QiniuUploadUtils.deleteFile:QiniuException]", var3);
    }

  }

  public void refreshCdn(String[] urls) {
    try {
      this.cdnManager.refreshUrls(urls);
    } catch (QiniuException var3) {
      var3.printStackTrace();
    }

  }

  private String getUpToken1(String fileName) {
    return this.auth.uploadToken(this.bucket, fileName);
  }

  private boolean upload(byte[] data, String targetName, String upToken) {
    try {
      Response res = this.uploadManager.put(data, targetName, upToken);
      return res.isOK();
    } catch (QiniuException var6) {
      Response response = var6.response;
      log.error("[QiniuUploadUtils.upload:QiniuException.response]", response);
      log.error("[QiniuUploadUtils.upload:QiniuException]", var6);
      return false;
    }
  }

  private byte[] input2byte(InputStream inStream) {
    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100];
    boolean var4 = false;

    int rc;
    try {
      while ((rc = inStream.read(buff, 0, 100)) > 0) {
        swapStream.write(buff, 0, rc);
      }
    } catch (IOException var6) {
      log.error("[QiniuUploadUtils.input2byte:IOException]", var6);
    }

    byte[] in2b = swapStream.toByteArray();
    return in2b;
  }

  public static void main(String[] arg) {
    QiniuUploadUtils quhtml = new QiniuUploadUtils("http://ok16l3v2m.bkt.clouddn.com/", "yinzihtml");
    quhtml.deleteFile("111");
  }
}
